package com.yunji.web.cabinet;

import com.yunji.base.business.UpgradeFileBusiness;
import com.yunji.base.dto.UpgradeFileDTO;
import com.yunji.base.vo.UpgradeFileVO;
import com.yunji.cabinet.params.CabinetVersionParam;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "管理端-版本文件")
@RestController
@RequestMapping("/upgrade/file")
public class UpgradeFileController {

    @Resource
    private UpgradeFileBusiness business;

    @ApiOperation("存入版本文件")
    @PreAuthorize("@ss.hasPermi('upgrade:file:add')")
    @PostMapping("saveVersionFile")
    public Response<Long> cabinetVersionFile(@Valid UpgradeFileDTO dto) {
        return ResponseHelper.success(business.cabinetVersionFile(dto));
    }

    @ApiOperation("查看版本文件列表")
    @PostMapping("search")
    public Response<List<UpgradeFileVO>> search(@RequestBody CabinetVersionParam param) {
        List<UpgradeFileVO> list = business.search(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("获取文件详情")
    @GetMapping("/getInfo/{fileId}")
    public Response<UpgradeFileVO> getInfo(@PathVariable("fileId") Long fileId) {
        return ResponseHelper.success(business.getInfo(fileId));
    }

    @ApiOperation("修改版本文件")
    @PreAuthorize("@ss.hasPermi('upgrade:file:edit')")
    @PostMapping("/update")
    public Response<String> update(@Valid UpgradeFileDTO dto) {
        return ResponseHelper.handle(business.update(dto),"修改失败");
    }

    @ApiOperation("删除版本文件")
    @PreAuthorize("@ss.hasPermi('upgrade:file:delete')")
    @DeleteMapping("/delete")
    public Response<String> delete(@RequestParam("fileId") Long fileId) {
        return ResponseHelper.handle(business.delete(fileId),"删除失败");
    }

    @ApiOperation("下载升级文件")
    @PostMapping("/upload")
    public Response<byte[]> download(@RequestParam("fileId") Long fileId) {
        return ResponseHelper.success(business.download(fileId));
    }

}
